package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Category;
import com.service.CategoryService;

@Controller @RequestMapping("/category")
public class CategoryController {

		@Resource
		CategoryController categoryController;
		
		@Resource 
		CategoryService categoryService; 
		
		@RequestMapping("")
		public String index(ModelMap m) {
		List<Category> categories=categoryService.listCategory();
		m.put("categoryList", categories);
			
			
			return "Category/Category";
		}
		@RequestMapping(value="/insert")
		public String addCategory(Category category,ModelMap m) {
		Category tempCategory=categoryService.getCategoryByName(category.getCategoryName());	
		if(tempCategory==null)
		{categoryService.addCategory(category);
		return "redirect:/category";}
		else
		{m.put("msg", "已有此标签");
		return "redirect:/category";
		}
		
		
		}	
		@RequestMapping("/edit/{name}")                  
		public String edit(@PathVariable("name") String name,ModelMap m) {
			Category c=categoryService.getCategoryByName(name);
			m.put("category",c);
			m.put("category_id", c.getCategoryId());
					//转向
			return "Category/edit";
		}
		
		
}
